package com.lzy.cloud.payment.facade;

import com.lzy.cloud.payment.constants.BusinessStatus;
import com.lzy.cloud.payment.dtos.pay.MobileRequestDTO;
import com.lzy.cloud.payment.dtos.payment.MsgOrderRefundDto;
import com.lzy.cloud.payment.dtos.payment.PayBillDto;
import com.lzy.cloud.payment.remote.AccountApi;
import com.lzy.cloud.payment.remote.dtos.AccountWalletDto;
import com.lzy.cloud.payment.remote.dtos.AccountWalletInfoDto;
import com.lzy.cloud.payment.service.PayBillService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class PayRefundInfoFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(PayRefundInfoFacade.class);

    @Autowired
    PayFacade payFacade;

    @Autowired
    AccountApi accountApi;

    @Autowired
    PayBillService payBillService;
    /**
     * 在线支付退款
     * @param morDto
     */
    public void onlineRefund(MsgOrderRefundDto morDto){
        if (morDto != null && !BigDecimalUtil.isZeroOrNull(morDto.getReturnOnline())) {
            MobileRequestDTO result = new MobileRequestDTO();
            // 退款
            try {
                LOGGER.info("-------退款开始----------");
                LOGGER.info("订单退款MSG======开始======操作日期: " + new Date() + " 订单号: " + morDto.getOrderId() + " 退款金额: "
                        + morDto.getReturnOnline());
                result.setOrderId(morDto.getOrderId() + "");
                result.setRefundAmount(morDto.getReturnOnline().toString());
                result.setRequestType(BusinessStatus.BusinessType.applyPayRefund);
                LOGGER.info("-------准备进入----------");
                payFacade.payRequestAll(result);
                LOGGER.info("订单退款======成功======订单号: " + result.getOrderId());
            } catch (Exception e) {
                e.printStackTrace();
                // TODO 失败了，怎么处理
                LOGGER.error("订单退款======失败======订单号: " + result.getOrderId());
            }
        }
    }

    /**
     * 余额支付退款
     * @param morDto
     */
    public void balanceRefund(MsgOrderRefundDto morDto){
        if (morDto != null && !BigDecimalUtil.isZeroOrNull(morDto.getReturnBalance())) {
            //根据会员id和shopId获取钱包id
            AccountWalletDto accountWalletDto = DozerUtils.map(accountApi.findByMemberIdAndPlatformId(morDto.getMemberId(),morDto.getShopId()),AccountWalletDto.class);
            if(accountWalletDto != null && accountWalletDto.getId() != null){
                AccountWalletInfoDto awiDto = DozerUtils.map(accountApi.selectAvailableAccount(accountWalletDto.getId()),AccountWalletInfoDto.class);
                if (awiDto != null && awiDto.getBalance() != null){
                    //将余额支付退款退回账户
                    BigDecimal amount = awiDto.getBalance().add(morDto.getReturnBalance());
                    awiDto.setBalance(amount);
                    Boolean ok = DozerUtils.map(accountApi.updateByPrimaryKey(awiDto).getResult(),Boolean.class);
                    Ensure.that(ok).isTrue("09000106");
                    //将退款记录表中添加记录
                    PayBillDto payBillDTO = new PayBillDto();
                    payBillDTO.setMemberId(morDto.getMemberId());
                    payBillDTO.setOrderId(morDto.getOrderId());
                    payBillDTO.setType(18);
                    payBillDTO.setAmount(morDto.getReturnBalance());
                    Ensure.that(payBillService.createPayBill(payBillDTO)).isTrue("09000107");
                }
            }
        }
    }


}
